package com.example.demo;

import com.example.demo.annotation.TxTransaction;
import com.example.demo.context.TxTransactionConstants;
import com.example.demo.context.TxTransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author yuaneg 2018/12/12
 */
@RestController
public class TestController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @TxTransaction
    @RequestMapping("/**")
    public Object aaa() {
        jdbcTemplate.execute("update test1 set name =name+1 where id = 1");
        return 1;
    }


    @RequestMapping("/commit")
    public Object commit(@RequestParam("id") String id) {
        stringRedisTemplate.convertAndSend(TxTransactionConstants.TX_NOTIFY_CHAT_SUCCESS, id);
        return jdbcTemplate.queryForList("select * from test1");
    }


    @RequestMapping("/commit2")
    public Object commi2t(@RequestParam("id") String id) {
        stringRedisTemplate.convertAndSend(TxTransactionConstants.TX_NOTIFY_CHAT_FAIL, id);
        return jdbcTemplate.queryForList("select * from test1");
    }
}
